import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class runRWmap {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService readPool = Executors.newFixedThreadPool(5);
        ExecutorService writePool = Executors.newFixedThreadPool(5);
        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        RWHashMap<String,Integer> rwHashMap = new RWHashMap<String,Integer>(16);
        for(int i=0;i<2;i++){
            writePool.execute(new Runnable() {
                @Override
                public void run() {

                    synchronized (lock) {
//                        lock.writeLock().unlock();
                        for (int i = 0; i < 20; i++) {
                            String key = Thread.currentThread().getId() + "-" + i;
                            rwHashMap.put(key, i);
                        }
                    }
//                    lock.writeLock().unlock();
                }
            });
        }
        for(int i =0;i<2;i++){
            readPool.execute(new Runnable() {
                @Override
                public void run() {
//                    lock.readLock().lock();
                    for(Map.Entry<String,Integer> entry:rwHashMap.entrySet()){
                        System.out.println(Thread.currentThread().getId()+"读: "+entry.getKey());
                    }
//                    lock.readLock().unlock();
                }
            });
            readPool.shutdown();
            writePool.shutdown();
        }


    }
}
